package com.xiaoq.parking.endpoint;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.coms.util.IdGenerator;
import com.xiaoq.parking.entity.ParkingEntity;
import com.xiaoq.parking.service.IParkingService;
import com.xiaoq.parking.vo.ParkingVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/api/v1/parking")
@Api(tags={"停车场相关接口"})
public class ParkingEndpoint {
	
	@Autowired
	private IdGenerator idGenerator;
	
	@Autowired
	private IParkingService parkingSer;
	
	private static final class ParkingListResponse extends GenericListResponse<ParkingVo> {}
	
	@ApiOperation(value = "爬虫添加停车场信息", notes = "爬虫添加停车场信息")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "爬虫添加停车场信息") })
	@PostMapping(value = "/reptile", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> addParking(
			@RequestBody @ApiParam(value = "parkingVo", required = true) ParkingVo parkingVo,HttpServletRequest request) {
				
		ParkingEntity parking = new ParkingEntity();
		BeanUtils.copyProperties(parkingVo, parking);
		parking.setId(idGenerator.nextId());
		parkingSer.addParking(parking);
		return GenericResponse.ok();

	}
	
	@ApiOperation(value = "停车场列表", notes = "停车场列表")
	@ApiResponses({ @ApiResponse(code = 200, response = ParkingListResponse.class, message = "停车场列表") })
	@GetMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> listBySites(
			@RequestParam(value = "sitesId", required = false) @ApiParam(value = "景区Id") String sitesId,
			@RequestParam(value = "latitude", required = true) @ApiParam(value = "当前经度", required = true) Double latitude,
			@RequestParam(value = "longitude", required = true) @ApiParam(value = "当前维度", required = true) Double longitude) {
		List<ParkingEntity> parkingList = parkingSer.findParkings(latitude, longitude, sitesId);
		List<ParkingVo> parkingVos = new ArrayList<>();
		ParkingVo parkingVo = null;
		for(ParkingEntity parking:parkingList){
			parkingVo = new ParkingVo();
			BeanUtils.copyProperties(parking, parkingVo);
			parkingVo.setRemainAmount(0);
			parkingVos.add(parkingVo);
		}
		
		return ParkingListResponse.listNoCount(parkingVos);
		
		

	}

}
